/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Converter;

import Controller.CityHelper;
import Model.City;
import java.util.LinkedList;
import java.util.List;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;

/**
 *
 * @author Admin
 */
@FacesConverter("city")
public class CityConverter implements Converter {

    public static List<City> lstCities = new LinkedList<City>();

    public CityConverter() {
        CityHelper helper = new CityHelper();
        lstCities = helper.getAllCity();
    }

    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {
        City foundCity = null;
        for (City selectedCity : lstCities) {
            if (selectedCity.getCityname() == value) {
                foundCity = selectedCity;
                break;
            }
        }
        return foundCity;
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {
        String string = "";
        if (value != null) {
            string = String.valueOf(((City)value).getCityname());
        }
        return string;
    }
}
